package com.dhz.common.utils;

/**
 * HTML内容的基本工具类
 *
 * @author FY
 */
public class HtmlUtils {

    public static final String PATTERN_FILTER_ALL_HTML = "(<[^<]*?>)|(<[\\s]*?/[^<]*?>)|(<[^<]*?/[\\s]*?>)";
    public static final String PATTERN_FILTER_JS = "<\\s*script[^>]*>(.|[\\r\\n])*?<\\s*\\/script[^>]*>";
    public static final String PATTERN_FILTER_CSS = "<\\s*style[^>]*>(.|[\\r\\n])*?<\\s*\\/style[^>]*>";

    /**
     * 过滤掉传入的HTML字符串的HTML标签内容，只保留文字内容
     * @param sHtml 原始字符串
     * @return 过滤掉HTML标签的字符串
     */
    public static String filterHtml(String sHtml) {
        if (StrExtUtils.isEmpty(sHtml)) return StrExtUtils.EMPTY;
        return sHtml.replaceAll(PATTERN_FILTER_ALL_HTML, StrExtUtils.EMPTY);
    }

    /**
     * 过滤掉字符串中携带的JS脚本内容
     * @param sHtml 冤死字符串
     * @return 被过滤掉JAVASCRIPT的字符串
     */
    public static String filterJavaScript(String sHtml) {
        if (StrExtUtils.isEmpty(sHtml)) return StrExtUtils.EMPTY;
        return sHtml.replaceAll(PATTERN_FILTER_JS, StrExtUtils.EMPTY);
    }

}
